package com.twu.biblioteca;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * User: dbelsey
 * Date: 31/01/13
 * Time: 17:10
 */
public class TestIO extends IOHandler {

    private ByteArrayOutputStream mockOutputStream;

    public TestIO(InputStream inStream){
        this.mockOutputStream = new ByteArrayOutputStream();
        userInputStream = inStream;
        appOutputStream = new PrintStream(this.mockOutputStream);
        inputScanner =  new Scanner(userInputStream);
    }

    public TestIO(String inputString){
        this.mockOutputStream = new ByteArrayOutputStream();
        userInputStream = new ByteArrayInputStream(inputString.getBytes());
        appOutputStream = new PrintStream(this.mockOutputStream);
        inputScanner =  new Scanner(userInputStream);
    }

    public void setInputStream(String inputString){
        userInputStream = new ByteArrayInputStream(inputString.getBytes());
    }

    public String[] outputLines() {
        return this.mockOutputStream.toString().split("\\r?\\n");
    }

    public String getOutputLine(int n){
        String [] outputLines = outputLines();
        return outputLines[n];
    }

    public String getOutputLines(int startLine, int numberOfLines){
        String [] outputLines = outputLines();
        String outputLineBlock = "";
        for(int i = 0; i < numberOfLines; i++)  {
            if( i != 0)
                outputLineBlock += "\n";
            outputLineBlock += outputLines[startLine + i];
        }
        return outputLineBlock;
    }
}
